package library.image;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.text.TextUtils;

import java.lang.ref.SoftReference;

import library.http.HttpTask;
import library.utils.MD5Util;


/**
 * Created by Mr.Fan on 2016/2/18.
 */
public class BitmapCache {
    private static LruCache<String, CacheableBitmap> cache;

    static {
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cacheSize = maxMemory / 2;
        //cacheSize = cacheSize < 16 ? cacheSize : 16;

        cache = new LruCache<String, CacheableBitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, CacheableBitmap value) {
                return value.getSize();
            }
        };
    }

    private static class CacheableBitmap {
        final int mSize;
        final SoftReference<Bitmap> mReference;

        CacheableBitmap(Bitmap bitmap) {
            mReference = new SoftReference<Bitmap>(bitmap);
            mSize = bitmap == null ? 0 : bitmap.getRowBytes() * bitmap.getHeight();
        }

        public SoftReference<Bitmap> getReference() {
            return mReference;
        }

        public Bitmap get() {
            if (mReference != null)
                return mReference.get();
            return null;
        }

        public int getSize() {
            return mSize;
        }
    }

    public static String key(String from, String src) {
        String key = from + "" + src + "";
        String md5 = MD5Util.getMD5FromString(key);
        return TextUtils.isEmpty(md5) ? key : md5;
    }

    public static String key(BitmapOption option) {
        HttpTask httpTask = option.getHttpTask();
        String url = httpTask != null && httpTask.getUrl() != null ? httpTask.getUrl() : String.valueOf(option.getSrc());
        String key = option.getFrom() + "" + url + "";
        key += option.getWidth() > 0 ? "width" + option.getWidth() : "";
        key += option.getHeight() > 0 ? "height" + option.getHeight() : "";
        key += option.getCorner() != 0 ? "round" + option.getCorner() : "";
        key += option.getFlag() != null ? "flag" + option.getFlag() : "";
        key += option.isGrayScale() ? "grayScale" : "";
        String md5 = MD5Util.getMD5FromString(key);
        return TextUtils.isEmpty(md5) ? key : md5;
    }


    public static void add(String key, Bitmap bitmap) {
        if (!TextUtils.isEmpty(key) && get(key) == null && bitmap != null && !bitmap.isRecycled()) {
            final CacheableBitmap cacheableBitmap = new CacheableBitmap(bitmap);
            cache.put(key, cacheableBitmap);
        }
    }


    public static Bitmap get(String key) {
        final CacheableBitmap cacheableBitmap = cache.get(key);
        if (cacheableBitmap == null)
            return null;
        Bitmap bitmap = cacheableBitmap.get();
        if (bitmap == null || bitmap.isRecycled()) {
            cache.remove(key);
            return null;
        }
        return bitmap;
    }

    public synchronized void clear() {
        cache.evictAll();
    }

}
